//
//  NewsCollectionViewCell.m
//  项目
//
//  Created by 焦钟培 on 15-1-5.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsCollectionViewCell.h"
#import "ReadingRoomModel.h"
#import "FirstViewCell.h"
#import "SecondTableViewCell.h"
#import "ConnectModel.h"
#import "UIScrollView+MJRefresh.h"
#import "Reachability.h"


@implementation NewsCollectionViewCell
- (void)dealloc
{
    [_tableArray release];
    [_curStr release];
    [_allArray release];
    [_table release];
    [_array release];
    [_firstModel release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.allArray = [NSMutableArray array];
        self.firstModel = [[ReadingRoomModel alloc] init];
        _firstModel.os = @"ios";
        _firstModel.ver = @"2.2.0.0";
        _firstModel.op = @"gac";
        _firstModel.p = @"1";
        _firstModel.w = @"280";
        _firstModel.cFrom = @"";
        [_firstModel release];
        self.array = [NSMutableArray array];
        self.tableArray = [NSMutableArray array];
        _itm = 20;
        [self createA];
        [self SetUpRefresh];
    }
    return self;
}
- (void)createA
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    [self.contentView addSubview:_table];
    [_table release];
}
- (BOOL)judgeConnected
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            return NO;
        }
            break;
        case ReachableViaWiFi:
        {
            return YES;
        }
            break;
        case ReachableViaWWAN:
        {
            return YES;
        }
            break;
        default:
            break;
    }
}

- (void)alertS
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常请重新设置网络" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
    
}
#warning 解析
- (void)startParters
{
    BOOL judge = [self judgeConnected];
    if (!judge) {
        [self alertS];
        [self.table headerEndRefreshing];
        [self.table footerEndRefreshing];
        return;
    }

    [_allArray removeAllObjects];
    [ConnectModel connectModelWithUrlStr:[_array firstObject] parmaters:nil block:^(NSData *data)
     {
         [self.table headerEndRefreshing];
         [self.table footerEndRefreshing];
         
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray * arrays = [dic objectForKey:@"RCItem"];
         NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObject:arrays forKey:_curStr];
         [_allArray addObject:dic1];
         _firstModel.cid = [_array lastObject];
         [_table reloadData];
     }];
}






#warning tableViewCell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [_tableArray removeAllObjects];
    NSMutableDictionary * dic = [_allArray firstObject];
    NSArray *arrays = [dic objectForKey:[dic.allKeys firstObject]];
    [_tableArray addObjectsFromArray:arrays];
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary * dic = [_allArray firstObject];
    static NSString *name = @"newsCell";
    NSArray *array = [dic objectForKey:[dic.allKeys firstObject]];
    NSDictionary *dics =[array objectAtIndex:indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:name];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:name];
    }
    [cell.textLabel setText:[dics objectForKey:@"Tit"]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:18]];
    [cell.detailTextLabel setText:[dics objectForKey:@"SNName"]];
    [cell.detailTextLabel setFont:[UIFont systemFontOfSize:13]];
    [cell.detailTextLabel setTextColor:[UIColor grayColor]];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary * dic = [_allArray firstObject];
    if ([[dic.allKeys firstObject] isEqualToString:@"精品接口"])
    {
        return 75;
    }
    if ([[dic.allKeys firstObject] isEqualToString:@"转藏接口"]) {
        return 55;
    }
    return 50;
}
#warning table点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary * dic = [_allArray firstObject];
    NSArray *array = [dic objectForKey:[dic.allKeys firstObject]];
    NSDictionary *dics =[array objectAtIndex:indexPath.row];
    _firstModel.aid = [dics objectForKey:@"Aid"];
    NSDictionary *diction = [NSDictionary dictionaryWithObject:_firstModel forKey:@"key"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SHARE" object:self userInfo:diction];
}
// 上拉加载, 下拉刷新
- (void)SetUpRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    // #warning 自动刷新(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
    
    //2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    if ([self.tableArray count] < 200) {
        [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
    }
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.table.headerPullToRefreshText = @"下拉可以刷新了";
    self.table.headerReleaseToRefreshText = @"松开立即刷新了";
    self.table.headerRefreshingText = @"正在刷新中";
    
    self.table.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.table.footerReleaseToRefreshText = @"松开立即加载更多数据";
    self.table.footerRefreshingText = @"正在加载中";
}

- (void)headerRereshing
{
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self startParters];
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    });
}


- (void)footerRereshing
{
    
    NSString *str = [NSString stringWithFormat:@"dn=%ld",_itm];
    _itm += 20;
    if ([self.tableArray count] >= 200) {
        [self.table footerEndRefreshing];
    } else {
        //        self.urlModel.page += 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            NSString *string = [NSString stringWithFormat:@"dn=%ld",_itm];
            NSString * path = [[_array firstObject] stringByReplacingOccurrencesOfString:str withString:string];
            NSString * last = [_array lastObject];
            [_array removeAllObjects];
            [_array addObject:path];
            [_array addObject:last];
            [self startParters];
            
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        });
        
    }
    
}


@end
